/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.entity.bo.WorkBo;
import com.yiqi.liebang.feature.mine.contract.WorkContract;
import com.yiqi.liebang.feature.mine.dependencies.work.DaggerWorkPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class WorkPresenter implements WorkContract.Presenter {

    private WorkContract.View mView;

    @Inject

    WorkContract.Model mModel;


    public WorkPresenter(WorkContract.View mView) {
        this.mView = mView;
        DaggerWorkPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void userAddWork(String comLogo, String company, String position, String beginTime, String endTime, String describeInfo) {
        if (TextUtils.isEmpty(comLogo)) {
            mView.showError("请选择公司LOGO！");
            return;
        }
        if (TextUtils.isEmpty(company)) {
            mView.showError("请公司名称！");
            return;
        }
        if (TextUtils.isEmpty(position)) {
            mView.showError("请输入职位名称！");
            return;
        }
        if (TextUtils.isEmpty(beginTime)) {
            mView.showError("请选择开始时间！");
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            mView.showError("请选择结束时间！");
            return;
        }

        if (DateUtil.getTimeCompareSize(beginTime,
                endTime) !=3) {
            mView.showError("结束时间必须大于开始时间！");
            return;
        }
        if (TextUtils.isEmpty(describeInfo)) {
            mView.showError("请添加工作经历描述！");
            return;
        }
        mModel.userAddWork(comLogo, company, position, beginTime, endTime, describeInfo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAddWorkSuccess();
//                T.showShort("添加成功!");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userEditWork(String id, String comLogo, String company, String position, String beginTime, String endTime, String describeInfo) {
        if (TextUtils.isEmpty(company)) {
            mView.showError("请公司名称！");
            return;
        }
        if (TextUtils.isEmpty(position)) {
            mView.showError("请输入职位名称！");
            return;
        }
        if (TextUtils.isEmpty(beginTime)) {
            mView.showError("请选择开始时间！");
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            mView.showError("请选择结束时间！");
            return;
        }
        if (!endTime.equals("至今")){
            if (DateUtil.getTimeCompareSize(beginTime,
                    endTime) !=3) {
                mView.showError("结束时间必须大于开始时间！");
                return;
            }
        }
        if (TextUtils.isEmpty(describeInfo)) {
            mView.showError("请添加工作经历描述！");
            return;
        }
        mModel.userEditWork(id, comLogo, company, position, beginTime, endTime, describeInfo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAddWorkSuccess();
//                T.showShort("修改成功!");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userGetWork(String id) {
        mModel.userGetWork(new WorkBo(id)).compose(mView.bindToLife()).subscribe(new Observer<WorkBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(WorkBo s) {
                mView.showWorkSuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userDeleteWork(String id, int sign) {
        mModel.userDeleteWork(new WorkBo(id, sign))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.showAddWorkSuccess();
//                        T.showShort("删除成功!");
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e.getMessage());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
